package com.itheima.homework;

import java.util.Collections;
import java.util.TreeSet;

/**
 * @Author 王加鑫
 * @Date 2021/5/14 19:14
 * @Version 1.0
 * 2、【编号：1505】现有若干图书信息（包含名称title、作者author、定价price）需要存储到set集合中，
 * 保证集合中无重复元素，并遍历查看。可以认为所有信息都相同的图书为重复数据。
 */
public class Demo2 {
    public static void main(String[] args) {
        Book book1 = new Book("三国演义","罗贯中",66.8);
        Book book2 = new Book("水浒传","施耐庵",62.8);
        Book book3 = new Book("红楼梦","曹雪芹",70.8);
        Book book4 = new Book("西游记","吴承恩",66.8);
        Book book5 = new Book("红楼梦","曹雪芹",70.8);
        TreeSet<Book> set = new TreeSet<>(
                (o1, o2) -> {
                    double res = o1.getPrice() - o2.getPrice();
                    if(res == 0){
                        res = o1.getTitle().compareTo(o2.getTitle());
                        if(res == 0){
                            res = o1.getAuthor().compareTo(o2.getAuthor());
                        }
                    }
                    int num = (int)res;
                    return num;
                }
        );
        Collections.addAll(set,book1,book2,book3,book4,book5);
        System.out.println(set);

    }
}

class Book{
    private String title;
    private String author;
    private double price;

    public Book() {
    }

    public Book(String title, String author, double price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}
